<?php
require APP_PATH.'Admin/Model/CategoryModel.class.php';
class IndexController extends Controller{
	public function __init(){
		$cate=new CategoryModel;
		$category=$cate->getAll();
		foreach($category as $k=>$v){
			if(isset($_GET['cid']) && $v['cid']==$_GET['cid']){
				$category[$k]['active']=" class='active' ";
			}else{
				$category[$k]['active']='';
			}
		}
		$this->assign('category',$category);
		//获得最新博客
		$db=new Model('blog');
		$sql="select * from blog order by bid desc limit 10";
		$newBlog=$db->query($sql);
		//获得热门博客
		$sql="select * from blog order by click desc limit 10";
		$hotBlog=$db->query($sql);
		$this->assign('newBlog',$newBlog);
		$this->assign('hotBlog',$hotBlog);
	}
	//默认首页
	public function index(){
		if(isset($_GET['cid'])){
			$where=' where cid='.$_GET['cid'];
		}else{
			$where='';
		}
		$db=new Model('blog');
		$sql1="select count(*) as c from blog $where";
		$count=$db->query($sql1);
		$page=new Page($count[0]['c'],2);
		$sql="select * from blog $where limit ".$page->limit();
		$data=$db->query($sql);
		$p=$page->show();
		$this->assign('data',$data);
		$this->assign('page',$p);
		$this->display('index.html');
	}
	//查看博客
	public function blog(){
		$bid=$_GET['bid'];
		$db=new Model('blog');
		$sql="select * from blog b join category c on b.cid=c.cid where b.bid=$bid";
		$data=$db->query($sql);
		// p($data);exit;
		$this->assign('data',$data[0]);
		$this->display('blog.html');
	}
	//获取点击数
	public function getClick(){
		$db=new Model('blog');
		$sql="update blog set click=click+1 where bid={$_GET['bid']}";
		$db->exe($sql);
		$click=$db->where("bid={$_GET['bid']}")->find();
		echo "document.write('{$click['click']}')";
	}
}
?>